import { NBorder, NFill, NFont } from '../../../types/style.interface';
import { getTextRect } from './get-text-rect';
import { formatFont } from './format-font-style';
import { limitThrowError } from '../../tools';
export type RichText = {font: NFont.IFont, text: string};
export type AterFormatRichText = {font: NFont.IFont, text: string, height: number, width: number};


export function richTextFormat(ctx: CanvasRenderingContext2D, value: RichText[], font: NFont.IFont, size: {w: number, h: number}, letterSpace: number = 1) {
    let _value = JSON.parse(JSON.stringify(value)) as RichText[];
    let richText: AterFormatRichText[][] = [];
    try {
        let checkCycleLoop = limitThrowError();
        if(value.length === 0) {
            return [];
        }
        if(font.autoWrap === true) {
            let rowInfo: {width: number, height: number, texts: any[], preTxtInfo: {w: number, h: number}} = {
                width: 0,
                height: 0,
                preTxtInfo: {w: 0, h: 0},
                texts: []
            }
            while(true) {
                if(_value.length === 0) {
                    if(rowInfo.texts.length > 0) {
                        richText.push(rowInfo.texts);
                    }
                    break;
                }
                let row = _value.pop();
                checkCycleLoop();
                if(row.text.length === 0) {
                    continue;
                }
                for(let i = 0; i < row.text.length; i++) {
                    let str = row.text.substring(0, i + 1);
                    let styleStr = formatFont(font.size, font.family, row.font.style, row.font.weight);
                    let {w: currentW, h: currentH } = getTextRect(ctx, str, styleStr);
                    rowInfo.preTxtInfo = {
                        w: currentW,
                        h: currentH
                    }
                    if(row.text[i] === '\n') {
                        let txt = row.text.substring(0, i);
                        let tempInfo = getTextRect(ctx, txt, styleStr);
                        rowInfo.texts.push({font: row.font, text: txt, size: tempInfo});
                        if(row.text.length !== i) {
                            _value.push({
                                text: row.text.substring(i+1),
                                font: row.font
                            });
                        }
                        richText.push(rowInfo.texts);
                        rowInfo = {
                            width: 0,
                            height: 0,
                            preTxtInfo: {w: 0, h: 0},
                            texts: []
                        }
                        break;
                    }
                    if(i === row.text.length - 1) {
                        rowInfo.texts.push(Object.assign({}, row, {size: {w: currentW, h: currentH}}));
                        if(rowInfo.height < currentH) {
                            rowInfo.height = currentH;
                        }
                        rowInfo.width += currentW;
                    } else {
                        if(currentW + rowInfo.width + rowInfo.texts.length * letterSpace > size.w) {
                            let breakIdx = i || 1;
                            let txt = row.text.substring(0, breakIdx);
                            let tempInfo = getTextRect(ctx, txt, styleStr);
                            rowInfo.texts.push({font: row.font, text: txt, size: tempInfo});
                            _value.push({
                                text: row.text.substring(breakIdx),
                                font: row.font
                            });
                            richText.push(rowInfo.texts);
                            rowInfo = {
                                width: 0,
                                height: 0,
                                preTxtInfo: {w: 0, h: 0},
                                texts: []
                            }
                            break;
                        }
                    }
                }
            }
        } else {
            // richText = [_value];
            for(let item of _value) {
                let rowTexts: any[] = [];
                if(item.text.indexOf('\n') !== -1) {
                    let strArr = item.text.split('\n');
                    for(let str of strArr) {
                        let styleStr = formatFont(font.size, font.family, item.font.style, item.font.weight);
                        let {w: currentW, h: currentH } = getTextRect(ctx, str, styleStr);
                        rowTexts.push({
                            text: str,
                            font: item.font,
                            size: {w: currentW, h: currentH}
                        });
                        richText.push(rowTexts);
                        rowTexts = [];
                    }
                } else {
                    let styleStr = formatFont(font.size, font.family, item.font.style, item.font.weight);
                    let {w: currentW, h: currentH } = getTextRect(ctx, item.text, styleStr);
                    rowTexts.push({
                        text: item.text,
                        font: item.font,
                        size: {w: currentW, h: currentH}
                    });
                    richText.push(rowTexts)
                }
                
            }
        }
        // console.log(richText)
        return richText;
    } catch (error) {
        console.log(error);
        return [];
    }

}